package com.anttribe.components.db.executor.formatter;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import lombok.Getter;

/**
 * @author zhaoyong
 * @date 2023/1/14 0014
 */
@Getter
public abstract class BaseTypeDataFormatter<T> implements DataFormatter<T> {

    public Class<T> getDataTypeClazz() {
        if (null != this.getClass().getGenericSuperclass()
            && this.getClass().getGenericSuperclass() instanceof ParameterizedType) {
            Type generalEnumValueType =
                ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
            try {
                return (Class<T>)Class.forName(generalEnumValueType.getTypeName(), false,
                    this.getClass().getClassLoader());
            } catch (ClassNotFoundException e) {
            }
        }

        return null;
    }

    @Override
    public String format(T data) {
        if (null == data) {
            return NULL_VALUE;
        }

        return formatNotNull(data);
    }

    /**
     * 格式化非NULL值
     * 
     * @param data
     *            数据
     * @return String
     */
    protected abstract String formatNotNull(T data);
}
